package com.ayoka.parenting.ui.group.manager;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.ayoka.parenting.R;
import com.ayoka.parenting.adapter.CircleGroupMemberManageAdapter;
import com.ayoka.parenting.bean.CircleMemberBean;
import com.ayoka.parenting.bean.PagerBean;
import com.ayoka.parenting.databinding.ActivityBaseRecyclerviewBinding;
import com.ayoka.parenting.mvp.MVPBaseRecyclerViewActivity;
import com.jincp.common.brvah.BaseQuickAdapter;
import com.jincp.common.brvah.BaseViewHolder;
import com.jincp.common.model.ToolBarOptions;

import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: MemberManagerActivity.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class MemberManagerActivity extends MVPBaseRecyclerViewActivity<CircleMemberBean, ActivityBaseRecyclerviewBinding, MemberManagerContract.View, MemberManagerPresenter> implements MemberManagerContract.View {


    public static void start(Activity activity, String topicId) {
        Intent intent = new Intent(activity, MemberManagerActivity.class);
        intent.putExtra("TopicId", topicId);
        activity.startActivity(intent);
    }

    private String topicId;

    @Override
    protected void configIntentValue() {
        Intent intent = getIntent();
        if (intent != null) {
            topicId = getIntent().getStringExtra("TopicId");
        }
    }

    @Override
    protected SwipeRefreshLayout getRefreshLayout() {
        return mDataBinding.swipeRefresh;
    }

    @Override
    protected RecyclerView getRecyclerView() {
        return mDataBinding.recyclerView;
    }

    @NonNull
    @Override
    protected BaseQuickAdapter<CircleMemberBean, BaseViewHolder> getAdapter() {
        CircleGroupMemberManageAdapter adapter = new CircleGroupMemberManageAdapter();
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                CircleMemberBean circleMemberBean = (CircleMemberBean) adapter.getItem(position);
                if (view.getId() == R.id.btn_agree) {
                    if (mPresenter != null) {
                        mPresenter.agreeApply(topicId, circleMemberBean.Uid, position);
                    }
                } else if (view.getId() == R.id.btn_ignore) {
                    if (mPresenter != null) {
                        mPresenter.deleteMember(topicId, circleMemberBean.Uid, position);
                    }
                } else if (view.getId() == R.id.btn_delete) {
                    if (mPresenter != null) {
                        mPresenter.deleteMember(topicId, circleMemberBean.Uid, position);
                    }
                }
            }
        });
        return adapter;
    }

    @Override
    protected void getData(boolean isShowWaitDialog) {
        if (mPresenter != null) {
            mPresenter.getCircleGroupMemberListData(isShowWaitDialog, topicId, pageNo, pageSize);
        }
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        ToolBarOptions options = new ToolBarOptions(R.string.member_manager);
        setCustomToolBar(mDataBinding.customToolbar.toolbar, options);

        getData(true);
    }

    @Override
    protected MemberManagerPresenter getPresenter() {
        return new MemberManagerPresenter();
    }

    @Override
    public void configCircleGroupMemberListData(PagerBean<CircleMemberBean> response) {
        if (response == null) return;
        setLoadMoreLayout(response.list, response.totalPage);
    }

    @Override
    public void onAgreeApplyCallback(boolean isSuccess, int position) {
        if (isSuccess){
            CircleMemberBean circleMemberBean =  adapter.getItem(position);
            circleMemberBean.Status = 1;
            adapter.notifyItemChanged(position);
        }
    }

    @Override
    public void onDeleteMemberCallback(boolean isSuccess, int position) {
        if (isSuccess){
            adapter.remove(position);
        }
    }
}
